package org.wanho.rent.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.wanho.jdbc.util.JdbcTemplate;
import org.wanho.jdbc.util.RowMapper;
import org.wanho.rent.constant.Constant;
import org.wanho.rent.dao.ProductDao;
import org.wanho.rent.entity.Product;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.factory.ObjectFactory;
import org.wanho.rent.util.CommonUtil;

public class ProductDaoImpl implements ProductDao {

	private RowMapper<Product> rm = null;
	private JdbcTemplate<Product> jt = (JdbcTemplate<Product>) ObjectFactory.getObject("jt");
	
	@Override
	public List<Product> selectByPage(String productTypeParam,
			String productDuration, String productRate, String productAmount,
			int pageNo, int pageSize) {
		StringBuffer sql = new StringBuffer()
			.append(" select ")
			.append(" 	p.productid, ")
			.append(" 	p.productname ")
			.append("  ")  // 此处省略。。。。。
			.append("  ")
			.append("  ")
			.append("  ")
			.append("  ")
			.append("  ")
			.append("  ")
			.append("  ")
			.append(" from ")
			.append(" 	product p left join company c on p.company=c.companyid ")
			.append(" 		left join producttype pt on p.producttype=pt.producttypeid ")
			.append(" 		left join lendingperiod lp on p.lendingperiod=lp.lendingperiodid ")
			.append(" where ")
			.append(" 	1=1 ");
		
		// 用集合来保存?对应的值
		List<Object> list = new ArrayList<Object>();
		
		// 限制状态
		sql.append(" and pt.productypestate = ?, ");
		sql.append(" and lp.periodstate = ? ");
		list.add(Constant.STATUS_ENABLE);
		list.add(Constant.STATUS_ENABLE);
		
		// 判断（动态SQL）
		if(!CommonUtil.isEmpty(productTypeParam) && !"全部".equals(productTypeParam) ){
			sql.append(" and pt.producttypename=? ");
			list.add(productTypeParam);
		}
		if(!CommonUtil.isEmpty(productDuration) && !"全部".equals(productDuration) ){
			sql.append(" and lp.period=? ");
			list.add(productDuration);
		}
		if(!CommonUtil.isEmpty(productRate) && !"全部".equals(productRate) ){
			if(productRate.contains("%以下")){
				sql.append(" and primelendingratefrom < ? ");
				list.add(Integer.parseInt(productRate.replace("%以下", "")));
			}else if(productRate.contains("%以上")){
				sql.append(" and primelendingrateto > ? ");
				list.add(Integer.parseInt(productRate.replace("%以上", "")));
			}else{
				sql.append(" and primelendingratefrom <= ? and primelendingrateto >= ? ");
				String[] arr = productRate.replace("%", "").split("-");
				list.add(Integer.parseInt(arr[0]));
				list.add(Integer.parseInt(arr[1]));
			}
			sql.append(" and lp.producttypename=? ");
		}
		if(!CommonUtil.isEmpty(productAmount) && !"全部".equals(productAmount) ){
			
			
		}
		
		// 分页
		sql.append(" limit ?,? ");
		list.add((pageNo-1)*pageSize);
		list.add(pageSize);
		
		
		return jt.query(sql.toString(), rm, list.toArray());
	}

}
